﻿using Deployment.Data.SQL.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.SQL.Connections.UI
{
    /// <summary>
    /// Interaction logic for SQLDatabaseControl.xaml
    /// </summary>
    public partial class SQLDatabaseControl : UserControl
    {
        public SQLDatabaseControl()
        {
            InitializeComponent();
        }

        private bool serverListRefreshStarted = false;
        private bool databaseListRefreshStarted = false;

        private void cbo_Servers_DropDownOpened(object sender, EventArgs e)
        {
            cbo_Servers.Cursor = Cursors.Wait;
            refreshServerList();
        }

        private void refreshServerList()
        {
            if (cbo_Servers.ItemsSource == null && btn_refresh.IsEnabled && !serverListRefreshStarted)
            {
                serverListRefreshStarted = true;
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += RefreshServerListWorker_DoWork;
                worker.RunWorkerCompleted += RefreshServerListWorker_RunWorkerCompleted;
                btn_refresh.IsEnabled = false;
                worker.RunWorkerAsync();
            }
        }

        void RefreshServerListWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = SQLServerHelpers.FindSQLServers();
        }

        void RefreshServerListWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                List<String> results = e.Result as List<String>;
                if (results != null)
                {
                    cbo_Servers.ItemsSource = results;
                }
            }
            btn_refresh.IsEnabled = true;
            cbo_Servers.Cursor = Cursors.Arrow;
            serverListRefreshStarted = false;
        }

        private void cbo_Databases_DropDownOpened(object sender, EventArgs e)
        {
            cbo_Databases.Cursor = Cursors.Wait;
            refreshDatabaseList();
        }

        private void refreshDatabaseList()
        {
            if (cbo_Databases.ItemsSource == null && !databaseListRefreshStarted)
            {
                databaseListRefreshStarted = true;
                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += RefreshDatabaseListWorker_DoWork;
                worker.RunWorkerCompleted += RefreshDatabaseListWorker_RunWorkerCompleted;
                Object[] args = new Object[] { cbo_Servers.Text, rbt_WinAuth.IsChecked, txt_user.Text, PasswordBox.Password };
                worker.RunWorkerAsync(args);
            }
        }

        private void RefreshDatabaseListWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                List<string> results = e.Result as List<string>;
                if (results != null)
                {
                    cbo_Databases.ItemsSource = results;
                }
            }
            cbo_Databases.Cursor = Cursors.Arrow;
            databaseListRefreshStarted = false;
        }

        private void RefreshDatabaseListWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (e.Argument != null && e.Argument is object[])
            {
                object[] args = e.Argument as object[];
                if(args.Length >= 4)
                {
                    string server = args[0] as string;
                    bool? WindowsAuth = args[1] as bool?;
                    string userId = args[2] as string;
                    string pswd = args[3] as string;
                    e.Result = SQLServerHelpers.GetAllDatabases(server, WindowsAuth, userId, pswd);
                    return;
                }
            }
            e.Cancel = true;
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            cbo_Servers.Cursor = Cursors.Wait;
            refreshServerList();
        }
    }
}
